var util = require('../../util/util.js');
var app = getApp();
Page({
  data: {
    // 是否显示搜索结果
    isShowSearchResult: false
  },
  onLoad: function (options) {
    // 豆瓣评分top250
    var top250 = app.g_api + 'movie/top250' + '?start=' + '0' + '&count=3',
    // 即将上映
      soonPlay = app.g_api + 'movie/coming_soon' + '?start=' + '0' + '&count=3',
      // 正在热映
      hotPlay = app.g_api + 'movie/in_theaters' + '?start=' + '0' + '&count=3',
      paramTop = 'top250',
      paramHot = 'hotPlay',
      paramSoon = 'soonPlay';
    this.getData(top250, paramTop, '豆瓣评分top250');
    this.getData(hotPlay, paramHot, '正在热映');
    this.getData(soonPlay, paramSoon, '即将上映');
  },
  // 获取数据,url，api地址，param数据类别key:即将上映，top250，搜索数据结果，正在热映等
  getData: function(url, param, title) {
    var that = this;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        "Content-Type": "json"
      },
      success: function (res) {
        var data = res.data,
        subjects = data.subjects;
        that.dealData(subjects, param, title)
      }
    })
  },
  // 对电影数据的处理并添加入data,subjects电影数据对象，param数据类别, title类别中文名
  dealData: function (subjects, param, title) {
    var movies = this.data[param] ? this.data[param].movies : [];
    for (var item in subjects) {
      var obj = subjects[item];
      // 对电影对象的标题长度进行控制，不超过6个字,超过则截取前6字后添加省略号
      var movieTitle = obj.title,
        len = movieTitle.length;
      if (len > 6) {
        movieTitle = movieTitle.slice(0, 6) + '...'
      }
      // 获取星星的状态数组
      var stars = util.getStarStatus(obj.rating.stars);
      var temp = {
        'title': movieTitle,
        'average': obj.rating.average,
        'picture': obj.images.large,
        'stars': stars
      }
      movies.push(temp);
    }
    var res = {};
    res[param] = {
      listTitle: title,
      movies: movies
    }
    this.setData(res)
    wx.hideNavigationBarLoading()
  },
  // 点击更多
  onTapMore: function(e) {
    var listTitle = e.currentTarget.dataset.listtitle;
    wx.navigateTo({
      url: './movie-more/movie-more?listTitle=' + listTitle
    })
  },
  intoDetail: function() {
    wx.navigateTo({
      url: './movie-detail/movie-detail'
    })
  },
  // 搜索
  onTapToSearch: function(e) {
    var val = e.detail.value;
    this.setData({
      keyValueForSearch: val,
      searchResult: '',
      isShowSearchResult: true
    });
    if (val !== '') {
      var api = app.g_api + '/movie/search?q=' + val + '&start=0&count=12';
      this.getData(api, 'searchResult')
    }
  },
  // 输入框失焦且没有输入内容则返回默认列表
  onBlur: function(e) {
    if (!e.detail.value) {
      this.setData({
        isShowSearchResult: false
      });
    }
  },
  onReachBottom: function(e) {
    var that = this,
    data = that.data;
    // 如果正显示搜索结果页
    if (data.isShowSearchResult) {
      var start = data.searchResult.movies.length;
      var api = app.g_api + '/movie/search?q=' + data.keyValueForSearch + '&start=' + start + '&count=12';
      this.getData(api, 'searchResult');
      wx.showNavigationBarLoading()
    }
  }
})